package com.dahaiwuliang.code.controller.admin;

import com.dahaiwuliang.code.pojo.Article;
import com.dahaiwuliang.code.pojo.User;
import com.dahaiwuliang.code.server.UserServer;
import com.dahaiwuliang.code.util.Consts;
import com.dahaiwuliang.code.util.CryptographyUtil;
import com.dahaiwuliang.code.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bzd
 */
@RestController
@RequestMapping (value = "/admin/user")
public class UserAdminController {

    @Autowired
    private UserServer userServer;

    @RequestMapping(value = "/list")
    @RequiresPermissions (value = "分页查询用户信息列表")
    public Map<String,Object> list(User s_user,

                                   @RequestParam(value = "latelyLoginTimes",required = false) String latelyLoginTimes,
                                   @RequestParam(value = "page",required = false) Integer page,
                                   @RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> resultMap=new HashMap<>();
        String s_bpublishDate = null;       //开始时间
        String s_epublishDate = null;       //结束时间
        if (StringUtil.isNotEmpty(latelyLoginTimes)){
            String[] str = latelyLoginTimes.split(" - ");       //拆分时间段
            s_bpublishDate = str[0];
            s_epublishDate = str[1];
        }
        resultMap.put("data",userServer.list(s_user,s_bpublishDate,s_epublishDate,page,pageSize, Sort.Direction.DESC,"registrationDate"));
        resultMap.put("total",userServer.getCount(s_user,s_bpublishDate,s_epublishDate));
        resultMap.put("errorNo",0);
        return resultMap;
    }

    /**
     * 修改是否是Vip
     * @param userId
     * @param isVip
     * @return
     */
    @RequestMapping(value = "/updateVipState")
    @RequiresPermissions (value = "修改是否是Vip")
  public Map<String,Object>  updateVipState(Integer userId,boolean isVip ){
        Map<String,Object> resultMap=new HashMap<>();
        User user=userServer.findById(userId);
        if (isVip){
            user.setVip(true);
        }else {
            user.setVip(false);
        }
        userServer.save(user);
        resultMap.put("success",true);
        return  resultMap;
    }
    @RequestMapping(value = "/updateUserState")
    @RequiresPermissions (value = "是否封禁用户")
    public Map<String,Object>  updateUserState(Integer userId,boolean isOff){
        Map<String,Object> resultMap=new HashMap<>();
        User user=userServer.findById(userId);
        if (isOff){
            user.setOff(true);
        }else {
            user.setOff(false);
        }
        userServer.save(user);
        resultMap.put("success",true);
        return  resultMap;
    }
    @ResponseBody
    @RequestMapping(value = "/resetPassword")
    @RequiresPermissions (value = "重置密码")
    public Map<String,Object>  resetPassword(Integer userId ){
        Map<String,Object> resultMap=new HashMap<>();
        User user=userServer.findById(userId);
        user.setPassword(CryptographyUtil.MD5("123456",CryptographyUtil.SALT));

        userServer.save(user);
        resultMap.put("errorNo",0);
        return  resultMap;
    }
    @ResponseBody
    @RequestMapping(value = "/addPoints")
    @RequiresPermissions (value = "修改积分")
    public Map<String,Object> addPoints(User user){
        Map<String,Object> resultMap=new HashMap<>();
        User olduser=userServer.findById(user.getUserId());
        olduser.setPoints(user.getPoints());
        userServer.save(olduser);
        resultMap.put("errorNo",0);
        return  resultMap;
    }
    @ResponseBody
    @RequestMapping(value = "/updateVipGrade")
    @RequiresPermissions (value = "修改vip等级")
    public Map<String,Object> updateVipGrade(User user){
        Map<String,Object> resultMap=new HashMap<>();
        User olduser=userServer.findById(user.getUserId());
        olduser.setVipGrade(user.getVipGrade());
        userServer.save(olduser);
        resultMap.put("errorNo",0);
        return  resultMap;
    }
    /**
     * 修改密码modifyPassword
     */
    @ResponseBody
    @RequestMapping(value = "/modifyPassword")
    @RequiresPermissions (value = "修改密码")
    public Map<String,Object> modifyPassword(@RequestParam(value = "oldPassword")String oldPassword,@RequestParam(value = "newPassword") String newPassword, HttpSession session){
        User user= (User) session.getAttribute(Consts.CURRENT_USER);
        Map<String,Object> resultMap=new HashMap<>();
        if (!user.getPassword().equals(CryptographyUtil.MD5(oldPassword,CryptographyUtil.SALT))){
            resultMap.put("success",false);
            resultMap.put("errorInfo","原密码错误");
            return  resultMap;
        }
        User olduser=userServer.findById(user.getUserId());
        olduser.setPassword(CryptographyUtil.MD5(newPassword,CryptographyUtil.SALT));
        userServer.save(olduser);

        resultMap.put("success",true);


        return  resultMap;
    }
}
